const values_default = {}

const domain_default = {}

const fields_default = {
  type: null,
  state: null,
  name: null,
  company_id: null,
  partner_id: null,
  partner_shipping_id: null,
  ref: null,
  invoice_date: null,
  invoice_date_due: null,
  invoice_user_id: null,
  invoice_origin: null,

  amount_total: null,
  amount_residual: null,
  invoice_outstanding_credits_debits_widget: null,
  invoice_payments_widget: null,
  invoice_has_outstanding: null,
  invoice_payment_state: null,

  invoice_line_ids: {
    product_id: null,
    name: null,
    account_id: null,
    price_unit: null,
    quantity: null,
    price_subtotal: null
  }
}

const filterItems = [
  { type: 'input', name: 'name___like', placeholder: '票号' },
  { type: 'date', name: 'invoice_date', placeholder: '日期' },
  { type: 'select', name: 'partner_id', placeholder: '客户' }
]

const columns = [
  {
    name: 'invoice_outstanding_credits_debits_widget',
    type: 'char',
    label: '未核销'
  },
  {
    name: 'invoice_payments_widget',
    type: 'char',
    label: '已核销'
  },

  { name: 'invoice_has_outstanding', type: 'boolean', label: '有未核销' },
  { name: 'invoice_payment_state__name', type: 'char', label: '支付状态' },

  { name: 'company_id__name', type: 'char', label: '公司' },
  { name: 'state__name', type: 'char', label: '状态' },
  { name: 'invoice_origin', type: 'char', label: '原单据' },
  { name: 'name', type: 'char', label: '票据号' },
  { name: 'invoice_date', type: 'char', label: '日期' },
  { name: 'invoice_date_due', type: 'char', label: '付款期限日期' },
  { name: 'partner_id__name', type: 'char', label: '客户' },
  { name: 'partner_shipping_id__name', type: 'char', label: '交货地址' },
  { name: 'invoice_user_id__name', type: 'char', label: '业务员' },
  { name: 'amount_total', type: 'char', label: '金额' },
  { name: 'amount_residual', type: 'char', label: '未付余额' }
]

const formItems = [
  // { type: 'input', name: 'name', label: '摘要' },
  { type: 'date', name: 'invoice_date', label: '日期' },
  { type: 'date', name: 'invoice_date_due', label: '付款期限日期' },
  {
    type: 'select',
    name: 'partner_id',
    label: '客户',
    placeholder: '客户'
  },
  {
    type: 'select',
    name: 'partner_shipping_id',
    label: '交货地址',
    placeholder: '交货地址'
  },
  {
    type: 'select',
    name: 'invoice_user_id',
    label: '业务员',
    placeholder: '业务员'
  }
]

const child_configs = {
  invoice_line_ids: {
    columns: [
      { name: 'product_id__name', type: 'char', label: '服务' },
      { name: 'name', type: 'char', label: '说明' },
      { name: 'account_id__name', type: 'char', label: '科目' },
      { name: 'quantity', type: 'char', label: '数量' },
      { name: 'price_unit', type: 'char', label: '单价' },
      { name: 'price_subtotal', type: 'char', label: '小记' }
    ],

    formItems: [
      { type: 'input', name: 'name', label: '说明' },
      {
        type: 'select',
        name: 'product_id',
        label: '服务',
        placeholder: '服务'
      },
      {
        type: 'select',
        name: 'account_id',
        label: '科目',
        placeholder: '科目'
      },
      {
        type: 'number',
        name: 'price_unit',
        label: '单价',
        precision: 2,
        step: 1,
        default: 0,
        placeholder: '单价'
      },
      {
        type: 'number',
        name: 'quantity',
        label: '数量',
        precision: 2,
        step: 1,
        default: 0,
        placeholder: '数量'
      }
    ]
  }
}

const workflow = {
  states: {
    draft: '草稿',
    posted: '已过账',
    cancel: '已取消'
  },

  actions: {
    update: { value: '', name: '修改', type: 'primary' },
    to_pay: {
      name: '登记付款',
      method: 'to_pay',
      last_in: ['posted'],
      hidden: params => {
        // eslint-disable-next-line object-curly-spacing
        // const { record = {} } = params
        // return record.invoice_status !== 'to invoice'
      },
      odoo_type: 'action',
      wizard: {
        model: 'account.payment.register',
        vals: {},
        context: {},
        fields: {
          payment_date: null,
          journal_id: null,
          payment_method_id: null,
          group_payment: null
        },
        formItems: [
          {
            type: 'select',
            name: 'payment_method_id',
            label: '付款方法',
            options: {
              fields: {},
              domain: {
                // TBD 需要 分别判断 是销售发票还是 采购账单
                // payment_type: 'inbound'
              }
            },
            options_for_select: params => {
              const { temp, items, activeRecord } = params
              // console.log('xxxxxxx, payment_method_id,', params)
              const journals = items.journal_id.options_for_select_init.filter(
                it => it.id === temp.journal_id
              )
              const journal = journals[0]
              const invoice_type = activeRecord.type

              if (['out_invoice', 'in_refund'].includes(invoice_type)) {
                return journal.inbound_payment_method_ids__objects
              } else {
                return journal.outbound_payment_method_ids__objects
              }
            }
          },
          {
            type: 'select',
            name: 'journal_id',
            label: '支付方式',
            options: {
              domain: ({ self }) => {
                const userinfo = self.get_userinfo()
                return { company_id: userinfo.company_id }
              },
              fields: {
                inbound_payment_method_ids: { name: null },
                outbound_payment_method_ids: { name: null }
              }
            }
          }
        ],
        method: 'create_payments'
      }
    },
    draft: {
      name: '重置为草稿',
      method: 'button_draft',
      last_in: ['cancel'],
      odoo_type: 'object',
      type: 'primary'
    },
    posted: {
      name: '确认',
      method: 'action_post',
      last_in: ['draft'],
      odoo_type: 'object',
      type: 'primary'
    },
    cancel: {
      name: '取消',
      last_in: ['draft', 'posted'],
      method: 'button_cancel',
      odoo_type: 'object'
    }
  },

  transfers: {
    draft: {
      actions: ['update', 'posted', 'cancel'],
      states: ['draft', 'posted']
    },
    posted: {
      actions: ['to_pay', 'cancel'],
      states: ['draft', 'posted']
    },
    cancel: {
      actions: ['draft'],
      states: ['draft', 'cancel']
    }
  }
}

const Model = {
  configs: {
    workflow,
    values_default,
    domain_default,
    fields_default,
    filterItems,
    columns,
    formItems,
    child_configs
  },

  extend: BaseClass => {
    class ModelClass extends BaseClass {
      global_domain(domain_src = {}) {
        // console.log('this.global_domain in odoo.res.partner')
        const domain = { ...domain_src }
        // const userinfo = self.get_userinfo()
        // const { uid } = userinfo
        // domain.user_id = uid
        return super.global_domain(domain)
      }
      async browse_one(id, query = {}) {
        const rec = await super.browse_one(id, query)

        const unpay = JSON.parse(rec.invoice_outstanding_credits_debits_widget)
        console.log('xxxxxx,browse_one ', rec, unpay)
        const payment = JSON.parse(rec.invoice_payments_widget)
        console.log('xxxxxx,browse_one ', payment)

        return rec
        //
      }
    }
    return ModelClass
  }
}

export default Model
